<template>
  <div class="side-bar-item" @click="itemClick">
    <div :class="{active: isActive}">     <!-- 将slot放在一个div里 使得动态设置的类样式生效 -->
      <slot></slot>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'SideBarItem',
    data() {
      return {
        // isActive: true
      }
    },
    props: {
      link: String
    },
    computed: {
      isActive () {
        // 当前点击的
        return this.$route.path.indexOf(this.link) !== -1;
      }
    },
    methods: {
      itemClick () {
        this.$router.replace(this.link)
      }
    },

  }
</script>

<style scoped>
  .side-bar-item {
    width: 160px;
    flex: 1;
    line-height: 48px;
  }

  .side-bar-item:hover {
    background-color: #F0F0F5;
    cursor: pointer;
  }
  .active {
    color: red;
    background-color: #F0F0F5;
    font-weight: bold;
  }
</style>
